/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filesystem;

import java.util.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author ArieS
 */
public class SAXHandler extends DefaultHandler {

    private Map result;
    private ArrayList<String> keys;
    private ArrayList values;
    
    public SAXHandler() {
        result = new HashMap();
        keys = new ArrayList();
        values = new ArrayList();
    }

    @Override
    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException {
        keys.add(qName);
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        StringBuilder value = new StringBuilder();

        
        for (int i = start; i < start+length; i++) {
            value.append(ch[i]);
        }

        if (value.length() != 0 && ! "\n".equals(value.toString())) {
            values.add(value);
        }
    }

    /**
     *
     * @return map where keys are the tag's names. 
     */
    public Map getResult() {
        int len = values.size();
        for(int i = 0; i < len; i++) {
            result.put(keys.get(i + 1), values.get(i));
        }
        return result;
    }
}